import CONFIG from "./config";
import axios from 'axios';
import {message} from "antd";
import {removeToken} from "./index";
const { basicProxy, appAddr, helpAddr, downloadAddr } = CONFIG;

export default function axiosMethod({
    type = "get", 
    baseUrl = "",  //接口部署地址
    url = "",      //接口相对地址
    body = {}, 
    header = {}, 
    timeout = 8000,
    callback = ()=>{},  //接口请求成功后用于接收响应数据的函数
    onUploadProgress = ()=>{},  //对上传事件进行监听，接收event对象，可以用于实现显示上传进度
    onDownloadProgress = ()=>{}  //对下载事件进行监听，接收event对象，可以用于实现显示下载进度
}){
    let query = {
        baseURL: baseUrl? baseUrl: window._BasicProxy? window._BasicProxy: basicProxy,
        method: type,
        url,
        timeout,
        responseType: 'json',
        onUploadProgress,
        onDownloadProgress,
        headers: {
            Authorization: `X-Token=${sessionStorage.getItem("token")}`,
            ...header
        }
    }
    if(type === "get" || query.headers["Content-type"] === "application/x-www-form-urlencoded"){
        query.params = body;
    } else {
        query.data = body;
    }
    typeof window.changeAppLoad === "function" &&
    body.load &&
    window.changeAppLoad(true); 
    axios(query)
    .then(res=>{
        typeof window.changeAppLoad === "function" &&
        body.load &&
        window.changeAppLoad(false); 
        
        if(res.status === 200){
            let result = res.data;
            if(result.StatusCode === 200 || result.code === "200"){
                callback(result.Data || result.data, result);
            } else {
                callback(result.Data || result.data, result);
                handleStatusCode(result.StatusCode || result.code, result.msg);
            }
        } else {
            handleStatusCode(res.status, res.response?.data?.msg||url);
        }
    })
    .catch(err=>{
        typeof window.changeAppLoad === "function" &&
        body.load &&
        window.changeAppLoad(false); 
        console.log(err, url);
        if(typeof err === "object" && err.response){
            handleStatusCode(err.response.status, err.response?.data?.msg||url);
        }
        if(typeof err === "object" && err.request){
            callback({}, {});
        }
    })
}

//对返回状态进行特殊处理，例如401， 403为掉线
const handleStatusCode = (data, msg) => {
    switch(data.toString()){
        case "400":
            message.warn(msg);
            break;
        case "401":
        case "403":
            message.warn(String(data) + msg + "你已掉线，请重新登录");
            setTimeout(() => {
                if(sessionStorage.getItem("baseUrl")){
                    sessionStorage.removeItem("token");
                    localStorage.removeItem("token");
                    window.location.href = sessionStorage.getItem("baseUrl") + `login.html?lg_sysid=260&lg_preurl=${encodeURIComponent(removeToken(window.location.href))}`; 
                }
            }, 1000);
            break;
        case "500":
            message.error(msg + "接口调用失败");
            break;
        default:
            message.warn(msg);
            break;
    }
}

